'''
Author: Donald duck tang5722917@163.com
Date: 2024-04-07 15:54:11
LastEditors: Donald duck tang5722917@163.com
LastEditTime: 2024-04-18 20:09:32
FilePath: \spice_netlist_front_end\src\Anasysis\cir_network.py
Description: 
Copyright (c) 2024 by Donald duck email: tang5722917@163.com, All Rights Reserved.
'''
import networkx as nx
from Circuit_element import Element_2port

def print_edge_elem(G):
    str = ''
    for edge in nx.edges(G):
        str += G[edge[0]][edge[1]]['name'] + ' '
    return str

def remove_graph_edge(G,F):
    G1 = G.copy()
    f_edges = F.edges()
    for edge in f_edges:
        if (nx.is_path(G1, edge)):
            G1.remove_edge(edge[0],edge[1])
    return G1

def build_cir_graoh(elem_obj_list,node_obj_list,):
    G = nx.Graph()
    for node in node_obj_list:
        if(node.leng_elem() == 0):
            continue
        G.add_node(node.print_node_name(),obj = node)

    for elem in elem_obj_list:
        if(isinstance(elem, Element_2port.Element_2port)):
            G.add_edge(elem.return_pos_node_name(),elem.return_neg_node_name(),name=elem.print_elem_name(),obj = elem)
            
    #取得节点对象 G.nodes['1']
    #取得边对象   G['3']['1']
    F= nx.dfs_tree(G,'1')
    G1 = remove_graph_edge(G,F)
    print(print_edge_elem(G))
    print(F.edges())
    print(print_edge_elem(G1))
    return G

def graph_loop_maxtrix(G):
    pass
